The R plugin for IntelliJ-based IDEs provides handy capabilities to work with the R Markdown files. To add a new R chunk,
library(ggplot2)
library(tidyr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
position the caret at any line or the code chunk, then click “+”.
The code chunk appears:
plot_median_quartiles <- function(file_path) {
# Read the CSV file
df <- read.csv(file_path, stringsAsFactors = FALSE)
# Convert Date column to Date type
df$Date <- as.Date(df$Date)
# Ensure all columns are numeric or NA, excluding the Date column
df_clean <- df %>%
mutate(across(-Date, ~ as.numeric(as.character(.)), .names = "numeric_{col}"))
# Drop non-numeric columns and keep only the Date and numeric columns
df_numeric <- df_clean %>%
select(Date, starts_with("numeric_")) %>%
rename_with(~ sub("numeric_", "", .))
# Convert to long format
df_long <- df_numeric %>%
pivot_longer(cols = -Date, names_to = "Bank", values_to = "Value")
# Compute median and quartiles by date
summary_stats <- df_long %>%
group_by(Date) %>%
summarise(
Median = median(Value, na.rm = TRUE),
Q1 = quantile(Value, 0.25, na.rm = TRUE),
Q3 = quantile(Value, 0.75, na.rm = TRUE),
.groups = 'drop'
)
# Plot using ggplot2
ggplot(summary_stats, aes(x = Date)) +
geom_line(aes(y = Median, color = "Median")) +
geom_ribbon(aes(ymin = Q1, ymax = Q3), alpha = 0.2, fill = "grey20") +
geom_vline(xintercept = as.Date("2018-01-01"), color = "red", linetype = "dashed") +
geom_vline(xintercept = as.Date("2018-04-01"), color = "pink", linetype = "dashed") +
labs(title = "Median and Quartiles of Values Over Time",
x = "Date",
y = "Value") +
theme_minimal() +
scale_color_manual(name = "Statistic", values = c("Median" = "blue")) +
theme(legend.position = "top")
}
plot_column <- function(filename, chosen_column) {
# Read the CSV file
data <- read.csv(filename, stringsAsFactors = FALSE)
# Convert the Date column to Date type
data$Date <- as.Date(data$Date)
# Check if the chosen column exists
if (!(chosen_column %in% colnames(data))) {
stop("Chosen column ", chosen_column, " does not exist in the CSV file.")
}
# Plot the chosen column
ggplot(data, aes(x = Date, y = get(chosen_column))) +
geom_line() +
geom_vline(xintercept = as.Date("2018-01-01"), color = "red", linetype = "dashed") +
geom_vline(xintercept = as.Date("2018-04-01"), color = "pink", linetype = "dashed") +
labs(title = paste("Time Series Plot of", chosen_column),
x = "Date",
y = chosen_column) +
theme_minimal()
}
Type any R code in the chunk, for example:
bank = "ПРИВАТБАНК"
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/2009_to_2017_quaterly/admin_expenses.csv', bank)
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/2009_to_2017_quaterly_diff/admin_expenses.csv', bank)
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/2018_to_now_monthly_differenced/admin_expenses.csv', bank)
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/admin_expenses.csv', bank)
bank = "ОЩАДБАНК"
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/2009_to_2017_quaterly/admin_expenses.csv', bank)
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/2009_to_2017_quaterly_diff/admin_expenses.csv', bank)
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/2018_to_now_monthly_differenced/admin_expenses.csv', bank)
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/admin_expenses.csv', bank)
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/admin_expenses.csv', "ПРИВАТБАНК")
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/admin_expenses.csv', "ОЩАДБАНК")
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/admin_expenses.csv', "УКРСИББАНК")
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/admin_expenses.csv', "УКРЕКСІМБАНК")
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/admin_expenses.csv', "УКРСОЦБАНК")
## Warning: Removed 18 rows containing missing values or values outside the scale range
## (`geom_line()`).
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/admin_expenses.csv', "ПРОМІНВЕСТБАНК")
## Warning: Removed 9 rows containing missing values or values outside the scale range
## (`geom_line()`).
# plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/admin_expenses.csv', "НАДРА")
# plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/admin_expenses.csv', "ФОРУМ")
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/admin_expenses.csv', "ПУМБ")
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/net_commision_income.csv', "ПРИВАТБАНК")
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/net_commision_income.csv', "ОЩАДБАНК")
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/net_commision_income.csv', "УКРСИББАНК")
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/net_commision_income.csv', "УКРЕКСІМБАНК")
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/net_commision_income.csv', "УКРСОЦБАНК")
## Warning: Removed 18 rows containing missing values or values outside the scale range
## (`geom_line()`).
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/net_commision_income.csv', "ПРОМІНВЕСТБАНК")
## Warning: Removed 9 rows containing missing values or values outside the scale range
## (`geom_line()`).
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/net_commision_income.csv', "НАДРА")
## Warning: Removed 36 rows containing missing values or values outside the scale range
## (`geom_line()`).
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/net_commision_income.csv', "ФОРУМ")
## Warning: Removed 40 rows containing missing values or values outside the scale range
## (`geom_line()`).
plot_column('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/net_commision_income.csv', "ПУМБ")
plot_median_quartiles('~/Documents/GitHub/MonetaryPolicyEffectOnNetInterestMargins/data/extracted/complete/total_assets.csv')
plot_median_quartiles("data/relative/admin_expenses_to_total_assets.csv")
Now, click the Run button on the chunk toolbar to execute the chunk code. The result should be placed under the chunk. Click the Knit and Open Document to build and preview an output.
plot_median_quartiles("data/relative/ovdp_to_total_assets.csv")
plot_median_quartiles("data/relative/net_commision_income_to_total_assets.csv")
plot_median_quartiles("data/relative/capital_to_total_assets.csv")
plot_median_quartiles("data/relative/net_interest_income_to_total_assets.csv")
plot_median_quartiles("data/relative/total_assets.csv")